package com.kede.api.stand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.stand.service.StoreService;
import com.kede.api.utils.OrderUtil;
import com.kede.api.utils.PubConst;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.UsersDomain;
import com.kede.domain.ZhiOrderDomain;
import com.kede.domain.ZhiUserCertifiedDomain;
import com.kede.ienum.store.CertiStatueEnum;
import com.kede.pojo.ResHttp;
import com.kede.service.UsersService;
import com.kede.service.ZhiOrderService;
import com.kede.service.ZhiUserCertifiedService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:Pr.li
 * @Date: create in 2023/6/18 11:33
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private UsersService usersService;
    @Resource
    private ZhiUserCertifiedService zhiUserCertifiedService;

    @Resource
    private ZhiOrderService zhiOrderService;

    @Override
    public ResHttp getStoreInfoByOpenid(String openid) {
        LambdaQueryWrapper<UsersDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UsersDomain::getXcxStoreOpenid,openid);
        UsersDomain user = usersService.getOne(queryWrapper);
        if (user == null){
            return ResHttp.code(201,"未注册用户");
        }
        ZhiUserCertifiedDomain userCertifiedDomain = zhiUserCertifiedService.getOneByUserId(user.getId());
        if(userCertifiedDomain == null){
            return ResHttp.code(202,"未提交门店信息");
        }
        if (userCertifiedDomain.getStatus().equals(CertiStatueEnum.REVIEW)
                && userCertifiedDomain.getIsPay().equals(BoolDictEnum.FALSE.getCode())){
            //要修改微信支付的订单号  因为同一个订单号不能去微信重复下
            LambdaQueryWrapper<ZhiOrderDomain > zquery = new LambdaQueryWrapper<>();
            zquery.eq(ZhiOrderDomain::getOrderSn,userCertifiedDomain.getOrderSn());
            ZhiOrderDomain zhiOrder = zhiOrderService.getOne(zquery);
            if(zhiOrder != null){
                String randomNo = OrderUtil.getRandomNo(PubConst.JOIN);
                zhiOrder.setOrderSn(randomNo);
                userCertifiedDomain.setOrderSn(randomNo);
                zhiOrderService.updateById(zhiOrder);
                zhiUserCertifiedService.updateById(userCertifiedDomain);
            }
            Map<String,String> map = new HashMap<>();
            map.put("money",userCertifiedDomain.getMoney().toString());
            map.put("order_sn",userCertifiedDomain.getOrderSn());
            return ResHttp.set(203,"提交门店信息未付款",map);
        }
        if (userCertifiedDomain.getStatus().equals(CertiStatueEnum.REVIEW)
                && userCertifiedDomain.getIsPay().equals(BoolDictEnum.TRUE.getCode())){
            return ResHttp.code(204,"待审核");
        }
        if (userCertifiedDomain.getStatus().equals(CertiStatueEnum.REFUSE)){
            return ResHttp.code(205,"审核拒绝");
        }
        if (userCertifiedDomain.getStatus().equals(CertiStatueEnum.APPROVED)){
            return ResHttp.code(2000,"审核通过");
        }
        return ResHttp.ok();
    }
}
